package com.driver.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

import static java.lang.Thread.sleep;

@RestController
@RequiredArgsConstructor
@Slf4j
public class TestController {

    @PostMapping("/validate-host")
    public Map<String, Object> validateHost() {
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("message", "Host is available");
        result.put("sessionToken", "sessionToken");
        return result;
    }

    @GetMapping("/get-map")
    public Map<String, Object> getMap() {
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("message", "Host is available");
        result.put("image", "http://localhost:8888/bj.jpg");
        return result;
    }

    @GetMapping("/weather/init")
    public Map<String, Object> getWeather() {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> data = new HashMap<>();
        
        Map<String, Object> sunlight = new HashMap<>();
        sunlight.put("color", new double[]{0.05, 0.05, 0.05, 1.0});
        sunlight.put("brightness", 1);
        data.put("sunlight", sunlight);

        Map<String, Object> fog = new HashMap<>();
        fog.put("color", new double[]{0.25, 0.25, 0.26, 1.0});
        fog.put("density", 0.06);
        fog.put("height", 0);
        fog.put("heightAttenuation", 0.39);
        fog.put("startDistance", 845);
        data.put("fog", fog);

        Map<String, Object> snow = new HashMap<>();
        snow.put("height", 4150);
        snow.put("speed", 140);
        snow.put("timeScale", 1.0);
        snow.put("size", new double[]{6, 18});
        snow.put("density", 15000);

        Map<String, Object> shape = new HashMap<>();
        shape.put("available", new String[]{"flake", "crystal", "powder"});
        shape.put("selected", "crystal");
        snow.put("shape", shape);

        data.put("snow", snow);
        result.put("success", true);
        result.put("data", data);
        return result;
    }

    @PostMapping("/weather/preview")
    public Map<String, Object> weatherPreview() {
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        return result;
    }

    @PostMapping("/weather/sunlight")
    public Map<String, Object> weatherSunlight() {
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        return result;
    }

    @PostMapping("/weather/fog")
    public Map<String, Object> weatherFog() {
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        return result;
    }

    @PostMapping("/weather/snow")
    public Map<String, Object> weatherSnow() {
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        return result;
    }

    @GetMapping("/geometry/list")
    public Map<String, Object> geometryList() {
        Map<String, Object> result = new HashMap<>();
        List<Map<String, Object>> data = new ArrayList<>();

        Map<String, Object> geometry1 = new HashMap<>();
        geometry1.put("id", 1);
        geometry1.put("name", "Geometry1");
        geometry1.put("category", "Polygon");
        List<Map<String, Double>> coordinates1 = new ArrayList<>();
        Map<String, Double> map11 = new HashMap<>();
        map11.put("x", 0.0);
        map11.put("y", 0.0);
        coordinates1.add(map11);
        Map<String, Double> map12 = new HashMap<>();
        map12.put("x", 0.10);
        map12.put("y", 0.0);
        coordinates1.add(map12);
        Map<String, Double> map13 = new HashMap<>();
        map13.put("x", 0.10);
        map13.put("y", 0.10);
        coordinates1.add(map13);
        Map<String, Double> map14 = new HashMap<>();
        map14.put("x", 0.0);
        map14.put("y", 0.10);
        coordinates1.add(map14);
        geometry1.put("coordinates", coordinates1);
        data.add(geometry1);

        Map<String, Object> geometry2 = new HashMap<>();
        geometry2.put("id", 2);
        geometry2.put("name", "Geometry2");
        geometry2.put("category", "Polyline");
        List<Map<String, Double>> coordinates2 = new ArrayList<>();
        Map<String, Double> map21 = new HashMap<>();
        map21.put("x", 0.0);
        map21.put("y", 0.0);
        coordinates2.add(map21);
        Map<String, Double> map22 = new HashMap<>();
        map22.put("x", 0.5);
        map22.put("y", 0.5);
        coordinates2.add(map22);
        Map<String, Double> map23 = new HashMap<>();
        map23.put("x", 0.10);
        map23.put("y", 0.0);
        coordinates2.add(map23);
        geometry2.put("coordinates", coordinates2);
        data.add(geometry2);

        Map<String, Object> geometry3 = new HashMap<>();
        geometry3.put("id", 3);
        geometry3.put("name", "Geometry3");
        geometry3.put("category", "PointSet");
        List<Map<String, Double>> coordinates3 = new ArrayList<>();
        Map<String, Double> map31 = new HashMap<>();
        map31.put("x", 0.2);
        map31.put("y", 0.2);
        coordinates3.add(map31);
        Map<String, Double> map32 = new HashMap<>();
        map32.put("x", 0.8);
        map32.put("y", 0.8);
        coordinates3.add(map32);
        Map<String, Double> map33 = new HashMap<>();
        map33.put("x", 0.6);
        map33.put("y", 0.3);
        coordinates3.add(map33);
        geometry3.put("coordinates", coordinates3);
        data.add(geometry3);

        result.put("success", true);
        result.put("data", data);
        return result;
    }

    @PostMapping("/geometry/rename")
    public Map<String, Object> renameGeometry() {
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        return result;
    }

    @PostMapping("/geometry/delete")
    public Map<String, Object> deleteGeometry() {
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        return result;
    }

    @PostMapping("/geometry/create")
    public Map<String, Object> createGeometry() {
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        return result;
    }

    @GetMapping("/object/models")
    public Map<String, Object> objectModels() {
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);

        Map<String, Object> data = new HashMap<>();
        data.put("name", "root");

        List<Map<String, Object>> children = new ArrayList<>();

        Map<String, Object> vehicles = new HashMap<>();
        vehicles.put("name", "Vehicles");
        List<Map<String, Object>> vehicleChildren = new ArrayList<>();
        Map<String, Object> car = new HashMap<>();
        car.put("modelId", 101);
        car.put("name", "Car");
        vehicleChildren.add(car);
        Map<String, Object> truck = new HashMap<>();
        truck.put("modelId", 102);
        truck.put("name", "Truck");
        vehicleChildren.add(truck);
        vehicles.put("children", vehicleChildren);
        children.add(vehicles);

        Map<String, Object> buildings = new HashMap<>();
        buildings.put("name", "Buildings");
        List<Map<String, Object>> buildingChildren = new ArrayList<>();
        Map<String, Object> house = new HashMap<>();
        house.put("modelId", 201);
        house.put("name", "House");
        buildingChildren.add(house);
        Map<String, Object> skyscraper = new HashMap<>();
        skyscraper.put("modelId", 202);
        skyscraper.put("name", "Skyscraper");
        buildingChildren.add(skyscraper);
        buildings.put("children", buildingChildren);
        children.add(buildings);

        Map<String, Object> furniture = new HashMap<>();
        furniture.put("name", "Furniture");
        List<Map<String, Object>> furnitureChildren = new ArrayList<>();
        Map<String, Object> chair = new HashMap<>();
        chair.put("modelId", 301);
        chair.put("name", "Chair");
        furnitureChildren.add(chair);
        Map<String, Object> table = new HashMap<>();
        table.put("modelId", 302);
        table.put("name", "Table");
        furnitureChildren.add(table);
        furniture.put("children", furnitureChildren);
        children.add(furniture);

        data.put("children", children);
        result.put("data", data);

        return result;
    }

    @PostMapping("/object/generate")
    public Map<String, Object> objectGenerate() {
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        List<Map<String, Object>> dataList = new ArrayList<>();

        Map<String, Object> object1 = new HashMap<>();
        object1.put("modelId", 101);
        Map<String, Double> position1 = new HashMap<>();
        position1.put("x", 10.0);
        position1.put("y", 20.0);
        object1.put("position", position1);
        Map<String, Double> rotation1 = new HashMap<>();
        rotation1.put("yaw", 45.0);
        rotation1.put("pitch", 10.0);
        rotation1.put("roll", 5.0);
        object1.put("rotation", rotation1);
        Map<String, Double> scale1 = new HashMap<>();
        scale1.put("x", 1.0);
        scale1.put("y", 1.0);
        scale1.put("z", 1.0);
        object1.put("scale", scale1);
        dataList.add(object1);

        Map<String, Object> object2 = new HashMap<>();
        object2.put("modelId", 102);
        Map<String, Double> position2 = new HashMap<>();
        position2.put("x", 40.0);
        position2.put("y", 50.0);
        object2.put("position", position2);
        Map<String, Double> rotation2 = new HashMap<>();
        rotation2.put("yaw", 60.0);
        rotation2.put("pitch", 15.0);
        rotation2.put("roll", 10.0);
        object2.put("rotation", rotation2);
        Map<String, Double> scale2 = new HashMap<>();
        scale2.put("x", 1.5);
        scale2.put("y", 1.5);
        scale2.put("z", 1.5);
        object2.put("scale", scale2);
        dataList.add(object2);

        result.put("data", dataList);
        return result;
    }

    @GetMapping("/object/list")
    public Map<String, Object> objectList() {
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        List<Map<String, Object>> dataList = new ArrayList<>();
        Map<String, Object> object1 = new HashMap<>();
        object1.put("id", 1);
        object1.put("modelId", 101);
        Map<String, Double> position1 = new HashMap<>();
        position1.put("x", 0.3);
        position1.put("y", 0.3);
        position1.put("z", 0.0);
        object1.put("position", position1);
        Map<String, Double> position2 = new HashMap<>();
        position2.put("yaw", 30.0);
        position2.put("pitch", 15.0);
        position2.put("roll", 5.0);
        object1.put("rotation", position2);
        Map<String, Double> position3 = new HashMap<>();
        position3.put("x", 1.0);
        position3.put("y", 1.0);
        position3.put("z", 1.0);
        object1.put("scale", position3);
        dataList.add(object1);
        Map<String, Object> object2 = new HashMap<>();
        object2.put("id", 2);
        object2.put("modelId", 102);
        Map<String, Double> position4 = new HashMap<>();
        position4.put("x", 0.7);
        position4.put("y", 0.7);
        position4.put("z", 0.3);
        object2.put("position", position4);
        Map<String, Double> position5 = new HashMap<>();
        position5.put("yaw", 45.0);
        position5.put("pitch", 0.0);
        position5.put("roll", 0.0);
        object2.put("rotation", position5);
        Map<String, Double> position6 = new HashMap<>();
        position6.put("x", 0.5);
        position6.put("y", 0.5);
        position6.put("z", 0.5);
        object2.put("scale", position6);
        dataList.add(object2);
        result.put("data", dataList);
        return result;
    }

    @PostMapping("/object/update")
    public Map<String, Object> updateObject() {
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        return result;
    }

    @PostMapping("/object/delete")
    public Map<String, Object> deleteObject() {
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        return result;
    }

    @GetMapping("/camera/list")
    public Map<String, Object> cameraList() {
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        List<Map<String, Object>> dataList = new ArrayList<>();
        Map<String, Object> object1 = new HashMap<>();
        object1.put("id", 1);
        object1.put("name", "Path 1");
        object1.put("isClosed", false);
        List<Map<String, Object>> viewList1 = new ArrayList<>();
        Map<String, Double> position1 = new HashMap<>();
        position1.put("x", 10.0);
        position1.put("y", 20.0);
        position1.put("z", 30.0);
        Map<String, Object> view1 = new HashMap<>();
        view1.put("position", position1);
        Map<String, Double> rotation1 = new HashMap<>();
        rotation1.put("yaw", 45.0);
        rotation1.put("pitch", 10.0);
        rotation1.put("roll", 5.0);
        view1.put("rotation", rotation1);
        viewList1.add(view1);
        Map<String, Double> position2 = new HashMap<>();
        position2.put("x", 15.0);
        position2.put("y", 25.0);
        position2.put("z", 35.0);
        Map<String, Object> view2 = new HashMap<>();
        view2.put("position", position2);
        Map<String, Double> rotation2 = new HashMap<>();
        rotation2.put("yaw", 50.0);
        rotation2.put("pitch", 15.0);
        rotation2.put("roll", 10.0);
        view2.put("rotation", rotation2);
        viewList1.add(view2);
        object1.put("views", viewList1);

        Map<String, Object> object2 = new HashMap<>();
        object2.put("id", 2);
        object2.put("name", "Path 2");
        object2.put("isClosed", true);
        List<Map<String, Object>> viewList2 = new ArrayList<>();
        Map<String, Double> position3 = new HashMap<>();
        position3.put("x", 40.0);
        position3.put("y", 50.0);
        position3.put("z", 60.0);
        Map<String, Object> view3 = new HashMap<>();
        view3.put("position", position3);
        Map<String, Double> rotation3 = new HashMap<>();
        rotation3.put("yaw", 60.0);
        rotation3.put("pitch", 15.0);
        rotation3.put("roll", 10.0);
        view3.put("rotation", rotation3);
        viewList2.add(view3);
        object2.put("views", viewList2);
        dataList.add(object1);
        dataList.add(object2);

        result.put("data", dataList);
        return result;
    }

    @PostMapping("/camera/update")
    public Map<String, Object> updateCamera() {
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        return result;
    }

    @PostMapping("/camera/delete")
    public Map<String, Object> deleteCamera() {
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        return result;
    }

    @PostMapping("/camera/create")
    public Map<String, Object> createCamera() {
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        return result;
    }

    @GetMapping("/camera/current-pose")
    public Map<String, Object> cameraPointAdd() {
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);

        Map<String, Double> position1 = new HashMap<>();
        position1.put("x", 10.0);
        position1.put("y", 40.0);
        position1.put("z", 30.0);
        Map<String, Object> view1 = new HashMap<>();
        view1.put("position", position1);
        Map<String, Double> rotation1 = new HashMap<>();
        rotation1.put("yaw", 33.0);
        rotation1.put("pitch", 20.0);
        rotation1.put("roll", 6.0);
        view1.put("rotation", rotation1);

        result.put("data", view1);
        return result;
    }

    @PostMapping("/capture/preview")
    public Map<String, Object> previewCapture() throws InterruptedException {
        sleep(2000);
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        return result;
    }

    @GetMapping("/capture/status")
    public Map<String, Object> statusCapture() {
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        // 随机返回 true 或 false
        Random random = new Random();
        result.put("isReady", random.nextBoolean());
        return result;
    }

    @PostMapping("/capture/execute")
    public Map<String, Object> executeCapture() {
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("message", "Capture started successfully");
        result.put("taskId", "ahdjka");
        return result;
    }

    @GetMapping("/capture/status/{taskId}")
    public Map<String, Object> statusCapture(@PathVariable("taskId") String taskId) {
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        // 随机返回
        String[] statuses = {
                "pending",
                "running",
                "completed",
                "failed"
        };
        Random random = new Random();
        int randomIndex = random.nextInt(statuses.length);
        result.put("status", statuses[randomIndex]);
        if (randomIndex == 2) {
            result.put("downloadUrl", "http://localhost:8888/bj.jpg");
        }
        return result;
    }

}
